package ssh;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.commons.io.FileUtils;

/**
 * 
 * Description: 图形界面 <br/>
 * Date: 2017年10月31日 下午10:12:03 <br/>
 *
 * @author xuejianxin
 */
public class MainGui {
	private Frame frame;
	private MenuBar menuBar;
	private Menu menu;
	private MenuItem menuItem, openFileBtn, saveFileBtn;
	private TextArea ta;
	private JButton button;
	private String cmdFilePath;
	private static final String  BTN_TEXT="执行   ";

	public MainGui() {
		init();
		addEvents();
	}

	public void init() {
		frame = new Frame(
				"\u8fdc\u7a0bShell\u6267\u884c\u5de5\u5177 \u859b\u5efa\u65b0");
		frame.setBounds(300, 200, 600, 400);
		// 菜单栏
		menuBar = new MenuBar();
		// 菜单
		menu = new Menu("File");
		// 菜单项
		openFileBtn = new MenuItem("Open");
		saveFileBtn = new MenuItem("Save");
		menuItem = new MenuItem("Exit");
		// 菜单添加菜单项
		menu.add(openFileBtn);
		menu.add(saveFileBtn);
		menu.add(menuItem);
		// 菜单栏添加菜单
		menuBar.add(menu);
		frame.setMenuBar(menuBar);
		// 文本域
		ta = new TextArea();
		frame.add(ta);

		button = new JButton(BTN_TEXT);
		button.setEnabled(false);
		frame.add(button, BorderLayout.SOUTH);

		frame.setVisible(true);

	}

	public void addEvents() {
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// 菜单点击
		menuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		// 打开文件
		openFileBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				JFileChooser chooser = new JFileChooser();
				chooser.setCurrentDirectory(new File("."));// 设置当前目录
				chooser.setAcceptAllFileFilterUsed(false);
				// 显示所有文件
				chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
					public boolean accept(File f) {
						return f.isDirectory() || f.getName().endsWith(".rsh");
					}

					public String getDescription() {
						return "remote shell(*.rsh)";
					}
				});

				int returnVal = chooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					button.setEnabled(true);
					cmdFilePath = chooser.getSelectedFile().getAbsolutePath();
				}
				// 读取展示文件
				if (cmdFilePath == null) {
					return;
				}
				File file = new File(cmdFilePath);
				try {
					StringBuilder text = new StringBuilder();
					//text.append("#文件路径:" + cmdFilePath + "\r\n");
					text.append(FileUtils.readFileToString(file,
							CmdTool.DEFAULT_CHARSET));
					ta.setText(text.toString());
					button.setText(BTN_TEXT+cmdFilePath);
				} catch (Exception e1) {
					e1.printStackTrace();
				}

			}
		});

		// 保存按钮
		saveFileBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(null!=cmdFilePath){
					// 获取修改后的目录
					String cmdStr = ta.getText();
					File file = new File(cmdFilePath);
					String msg=null;
					try {
						FileUtils.writeStringToFile(file, cmdStr,
								CmdTool.DEFAULT_CHARSET);
						msg="保存成功!";
					} catch (IOException e1) {
						e1.printStackTrace();
						msg="保存失败!"+e1.getMessage();
					}
					JOptionPane.showMessageDialog(null, msg, "保存配置文件", JOptionPane.INFORMATION_MESSAGE); 
				}
			}
		});

		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String cmdStr = ta.getText();
					String[] cmdStrArr = cmdStr.split("\r\n");
					List<String> cmdList = new ArrayList<String>();
					for (String s : cmdStrArr) {
						cmdList.add(s);
					}
					CmdTool.parserAndExecute(cmdList);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MainGui();
	}

}